package com.js.nickcar.module.addcar.check;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.js.nickcar.R;
import com.js.nickcar.api.OptionConfig;
import com.js.nickcar.api.UrlConfig;
import com.js.nickcar.api.UrlUtils;
import com.js.nickcar.base.BaseFragment;
import com.js.nickcar.dialog.DialogHelper;
import com.js.nickcar.dialog.OptionDialog;
import com.js.nickcar.entity.local.BaseCarInfoEntity;
import com.js.nickcar.entity.remote.OptionBean;
import com.js.nickcar.module.addcar.uploadphoto.UploadCarPhotoFragment;

import java.util.List;
import java.util.Map;

import butterknife.Bind;

import static com.js.nickcar.dialog.DialogHelper.openOptionDialog;

/**
 * Created by LZY on 2017/10/5.
 */

public class CheckCarFragment extends BaseFragment<CheckCarPresenter>
        implements CheckCarView, OptionConfig, CheckCarAdapter.OnItemClickListener
        , BaseCarInfoEntity.Param, BaseCarInfoEntity.ItemType {

    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private static final String EXTRA_CAR_ID = "car_id";
    public OptionDialog optionDialog;
    public CheckCarAdapter mAdapter;

    private String[] optionKeys = {
            CAR_CHECK_LICENSE_TYPE,
            CAR_CHECK_TECH_STATUS,
            CAR_CHECK_FIX_STATUS,
            CAR_CHECK_USAGE,
            CAR_CHECK_STATUS
    };

    @Override
    protected void initBundle(Bundle bundle) {
        super.initBundle(bundle);
        mPresenter.setCarId(bundle.getString(EXTRA_CAR_ID));
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_base_car_info;
    }

    @Override
    protected CheckCarPresenter createPresenter() {
        return new CheckCarPresenter();
    }

    public static CheckCarFragment getInstance(String carId) {
        CheckCarFragment checkCarFragment = new CheckCarFragment();
        Bundle bundle = new Bundle();
        bundle.putString(CheckCarFragment.EXTRA_CAR_ID, carId);
        checkCarFragment.setArguments(bundle);
        return checkCarFragment;
    }

    @Override
    protected void initData() {
        mPresenter.getOptions(optionKeys);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext().getApplicationContext()));
        mAdapter = new CheckCarAdapter(getContext(), mPresenter.getData());
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected String getTitle() {
        return getString(R.string.check_car);
    }

    @Override
    public void onNorItemClick(BaseCarInfoEntity entity, int position) {
        switch (entity.getParam()) {
            case LICENSE_TYPE://证件类型
                openLicenseTypeDialog(entity, position);
                break;
            case TECH_STATUS://技术状况
                openTechStatusDialog(entity, position);
                break;
            case FIX_STATUS://维修保养
                openFixStatusDialog(entity, position);
                break;
            case USAGE://工作性质
                openUsageDialog(entity, position);
                break;
            case STATUS://综合车况
                openStatusDialog(entity, position);
                break;
        }
    }

    private void openLicenseTypeDialog(final BaseCarInfoEntity entity, final int position) {
        optionDialog = openOptionDialog(getContext(), mPresenter.getCheckLicenseTypeData(), mPresenter.getOptionCheckLicenseType(), new OptionDialog.OnConfirmListener() {
            @Override
            public void onConfirm(OptionBean option) {
                mPresenter.setOptionCheckLicenseType(option);
                entity.setContent(option.getValue());
                entity.setValue(option.getKvp_id());
                mAdapter.notifyItemChanged(position);
            }
        });
        optionDialog.setSpanCount(2);
    }


    private void openTechStatusDialog(final BaseCarInfoEntity entity, final int position) {
        optionDialog = DialogHelper.openOptionDialog(getContext(), mPresenter.getCheckTechStatusData(), mPresenter.getOptionCheckTechStatus(), new OptionDialog.OnConfirmListener() {
            @Override
            public void onConfirm(OptionBean option) {
                mPresenter.setOptionCheckTechStatus(option);
                entity.setContent(option.getDescription() + '(' + option.getValue() + ')');
                entity.setValue(option.getKvp_id());
                mAdapter.notifyItemChanged(position);
            }
        });
        optionDialog.setSpanCount(3);
    }

    private void openFixStatusDialog(final BaseCarInfoEntity entity, final int position) {
        optionDialog = openOptionDialog(getContext(), mPresenter.getCheckFixStatusData(), mPresenter.getOptionCheckFixStatus(), new OptionDialog.OnConfirmListener() {
            @Override
            public void onConfirm(OptionBean option) {
                mPresenter.setOptionCheckFixStatus(option);
                entity.setContent(option.getDescription() + '(' + option.getValue() + ')');
                entity.setValue(option.getKvp_id());
                mAdapter.notifyItemChanged(position);
            }
        });
        optionDialog.setSpanCount(3);
    }


    private void openUsageDialog(final BaseCarInfoEntity entity, final int position) {
        optionDialog = openOptionDialog(getContext(), mPresenter.getCheckUsageData(), mPresenter.getOptionCheckUsage(), new OptionDialog.OnConfirmListener() {
            @Override
            public void onConfirm(OptionBean option) {
                mPresenter.setOptionCheckUsage(option);
                entity.setContent(option.getDescription() + '(' + option.getValue() + ')');
                entity.setValue(option.getKvp_id());
                mAdapter.notifyItemChanged(position);
            }
        });
        optionDialog.setSpanCount(3);
    }

    private void openStatusDialog(final BaseCarInfoEntity entity, final int position) {
        optionDialog = openOptionDialog(getContext(), mPresenter.getCheckStatusData(), mPresenter.getOptionCheckStatus(), new OptionDialog.OnConfirmListener() {
            @Override
            public void onConfirm(OptionBean option) {
                mPresenter.setOptionCheckStatus(option);
                entity.setContent(option.getDescription());
                entity.setValue(option.getKvp_id());
                mAdapter.notifyItemChanged(position);
            }
        });
    }


    @Override
    public void onNext(List<BaseCarInfoEntity> lists) {
        for (BaseCarInfoEntity item : lists) {
            if (NEXT.equals(item.getParam())) continue;
            if (TextUtils.isEmpty(item.getContent())) {
                String msg = String.format(getString(R.string.base_car_info_msg),
                        getString(item.getType() == TYPE_SELECT
                                ? R.string.please_select : R.string.please_input),
                        item.getTitle());
                showToast(msg);
                return;
            }
        }

        //下一步提交车辆基本信息
        commitCheckCar(lists);
    }

    private void commitCheckCar(List<BaseCarInfoEntity> lists) {
        Map<String, String> map = UrlUtils.getBaseRequestMap(UrlConfig.API_APPRAISER_CAR_CHECK);
        map.put("car_id", mPresenter.getCarId());
        for (BaseCarInfoEntity entity : lists) {
            if (NEXT.equals(entity.getParam())) continue;
            if (TextUtils.isEmpty(entity.getContent())) {
                showToast(getString(entity.getType() == TYPE_SELECT
                        ? R.string.please_select : R.string.please_input) + entity.getTitle());
                return;
            }
            map.put(entity.getParam(), entity.getValue());
        }
        mPresenter.commitCheckCar(map);
    }

    @Override
    public void commitCheckCarSubcriberSuccess(String carId) {
        UploadCarPhotoFragment uploadCarPhotoFragment = UploadCarPhotoFragment.getInstance(carId);
        uploadCarPhotoFragment.setTargetFragment(this, getTargetRequestCode());
        addFragment(uploadCarPhotoFragment);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) return;
        if (requestCode == getTargetRequestCode()) {
            getTargetFragment().onActivityResult(requestCode, resultCode, data);
            finish();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DialogHelper.closeDialog(optionDialog);
    }
}
